package com.tarena.lbs.content.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.common.exception.ServiceException;
import com.tarena.lbs.common.response.StatusCode;
import com.tarena.lbs.content.dao.mapper.ArticleContentMapper;
import com.tarena.lbs.content.dao.repository.ArticleRepository;
import com.tarena.lbs.content.pojo.po.ArticleContentPO;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ArticleRepositoryImpl implements ArticleRepository {
    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Override
    public void save(ArticleContentPO param) {

        articleContentMapper.insert(param);
    }

    @Override
    public List<ArticleVO> selectList(ArticleQuery articleQuery) {
        QueryWrapper<ArticleContentPO> queryWrapper = new QueryWrapper<>();
        
        if (StringUtils.hasText(articleQuery.getArticleTitle())) {
            queryWrapper.like("article_title", articleQuery.getArticleTitle());
        }
        
        if (articleQuery.getArticleCategoryId() != null) {
            queryWrapper.eq("article_category_id", articleQuery.getArticleCategoryId());
        }
        
        if (StringUtils.hasText(articleQuery.getArticleLabel())) {
            queryWrapper.like("article_label", articleQuery.getArticleLabel());
        }
        
        if (articleQuery.getArticleStatus() != null && !articleQuery.getArticleStatus().isEmpty()) {
            queryWrapper.in("article_status", articleQuery.getArticleStatus());
        }
        
        if (articleQuery.getSource() != null) {
            queryWrapper.eq("source", articleQuery.getSource());
        }
        
        if (articleQuery.getActivityId() != null) {
            queryWrapper.eq("activity_id", articleQuery.getActivityId());
        }
        
        if (articleQuery.getUserId() != null) {
            queryWrapper.eq("user_id", articleQuery.getUserId());
        }
        
        if (articleQuery.getUpdateStartTime() != null) {
            queryWrapper.ge("update_time", articleQuery.getUpdateStartTime());
        }
        
        if (articleQuery.getUpdateEndTime() != null) {
            queryWrapper.le("update_time", articleQuery.getUpdateEndTime());
        }
        
        // 查询ArticleContentPO列表
        List<ArticleContentPO> apl = articleContentMapper.selectList(queryWrapper);
        
        // 转换为ArticleVO列表
        List<ArticleVO> avl = new ArrayList<>();
        for (ArticleContentPO po : apl) {
            ArticleVO vo = new ArticleVO();
            BeanUtils.copyProperties(po, vo);
            avl.add(vo);
        }
        if (avl.size() == 0){
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        return avl;
    }

    @Override
    public ArticleVO selectById(String id) {
        ArticleContentPO po = articleContentMapper.selectById(id);
        if (po != null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        ArticleVO vo = new ArticleVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }
}